import React, { Component } from 'react'
import {
    View,
    Text,
    Button,
    ActivityIndicator,
    FlatList
} from 'react-native'
import Style from '../utils/style/style'
import WXStyle from '../utils/style/wxstyle';
import * as fetch from '../utils/fetch'

import SearchInput from './input';
import DataList from '../views/DataList';
import PageLoading from './pageLoding'
export default class Purchase extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            keyword: '',
            dataList: null,
            pageNum: 0,
            hasMore: true,
            pageSize: 10,
        }
    }
    componentWillMount() {
        this._getData()
    }
    search(text) {
        this.setState({
            keyword: text
        })
    }

    _getData = () => {
        const api = this.props.api;//如果把分页组件化 this.props.api
        let pageNum = this.state.pageNum;
        pageNum = pageNum + 1;
        if (this.state.hasMore) {
            this.setState({
                pageNum: pageNum
            })
            let params = {
                pageNum: this.state.pageNum,
                pageSize: this.state.pageSize
            }
            if (this.state.keyword) {
                params['keyword'] = this.state.keyword
            }
            this._fetchData(api, params);
        }
    }
    _fetchData(api, params) {
        // fetch.request(api,'GET',params).then(data => {

        // })
        const pageSize = this.state.pageSize;
        if (this.state.pageNum < 4) {
            this.setState({ loading: true });
            setTimeout(() => {
                const data = [];
                let preData = this.state.dataList ? this.state.dataList : [];
                for (var i = 0; i < pageSize; i++) {
                    data.push({
                        purchase_id: (this.state.pageNum - 1) * pageSize + i + 1,
                        PurchaserId: 2,
                        TenantId: 2,
                        Code: '12345678923654859',
                        TotalPrice: 5632562,
                        ActualAmount: 5622314,
                        PurchaseTime: 1526884640000,
                        state: i % 3,
                        PNumber: 12,
                        species: 3,
                    })
                };
                this.setState({ dataList: preData.concat(data), loading: false })
            }, 3000)


        } else {
            this.setState({ hasMore: false, loading: false })
        }
    }
    _PageLoading = ()=>(
        <PageLoading
        hasMore={this.state.hasMore}
        loading={this.state.loading}
        />
    )
    // _renderItem=({item}) =>{
    //     // alert(item.purchase_id)
    //     this.props.renderItem
    // }
    
    render() {
        return (
            <View style={{ flex: 1 }}>
                <View style={[Style.searchWrapper]}>
                    <View style={[Style.centered]}>
                        <SearchInput search={this.search} />
                    </View>
                </View>
                {
                    this.state.loading && !this.state.dataList ?
                        <View style={{ flex: 1 }}>
                            <ActivityIndicator style={Style.centered} />
                        </View> : !this.state.loading && this.state.dataList.length == 0 ?
                            <View style={{ flex: 1 }}>
                                <Text>暂无数据</Text>
                            </View> :
                            <DataList
                                data={this.state.dataList}
                                renderItem={this.props.renderItem}
                                LoadMore={this._getData}
                                loading={this.state.loading}
                                hadMore={this.state.hasMore}
                                footer={this._PageLoading}
                                itemHeight={this.props.itemHeight}
                            />
                }

            </View>
        );
    }

}
